package sz.com.postoffice.jxyzwgcase.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import sz.com.postoffice.jxyzwgcase.service.OperationTargetService;
import sz.com.postoffice.jxyzwgcase.utils.DateStrUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Component
public class TaskCollectDataFromADO {

    private final OperationTargetService operationTargetService;

    public TaskCollectDataFromADO(OperationTargetService operationTargetService) {
        this.operationTargetService = operationTargetService;
    }

    @Scheduled(cron = "0 0 1 * * ?",zone = "Asia/Shanghai")//每天01:00:00
    public void collectDataFromADO() {
        Date date = new Date();//获取当天的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        //减少一天
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DAY_OF_MONTH, -1);
        Date dt1 = rightNow.getTime();
        //格式化
        String reStr = sdf.format(dt1);
        operationTargetService.collectDataFromADO(new String[]{reStr});
    }

    @Scheduled(cron = "0 30 1 ? * MON",zone = "Asia/Shanghai")//周一凌晨 01:30:00
    public void collectDataFromADO4Week() {
        Date date = new Date();//获取当天的时间
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DAY_OF_MONTH, +1);
        Date dt1 = rightNow.getTime();
        String[] split = DateStrUtil.getPrevWeekDate(dt1).split(",");
        List<String> days = DateStrUtil.getDays(split[0], split[1]);
        String[] reStr = days.toArray(new String[days.size()]);
        operationTargetService.collectDataFromADO(reStr);
    }

    @Scheduled(cron = "0 0 2 1 * ?",zone = "Asia/Shanghai")//每月1号 02:00:00
    public void collectDataFromADO4Month() {
        Date date = new Date();//获取当天的时间
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DAY_OF_MONTH, +1);
        Date dt1 = rightNow.getTime();
        String[] split = DateStrUtil.getPrevMonthDate(dt1).split(",");
        List<String> days = DateStrUtil.getDays(split[0], split[1]);
        String[] reStr = days.toArray(new String[days.size()]);
        operationTargetService.collectDataFromADO(reStr);
    }
}
